var BarcodeInputMethod = IBImgInputMethod.extend({
	menu_node: null,
	size_node: null,
	code_node: null,

	size_index: 1,
	code_index: 0,
	size_map_text: null,
	size_map: null,
	code_map: null,
	instruction_map: null,

	ext_size: null,
	ext_code: null,

	constructor: function()
	{
		var x = this;
		var size_handler = function(pos) { x.size_index = pos; x.UpdateUI(); return true; }
		var code_handler = function(pos) { x.code_index = pos; x.UpdateUI(); return true; }
		var i;

		this.size_map_text = new Array($GFS('Small'),$GFS('Medium'),$GFS('Large'),$GFS('Extra Large'));
		this.size_map = new Array(1,2,3,5);

		// Code node...
		this.code_map = new Array('EAN','UPC','ISBN','39','128','128B','128C','I25','128RAW','CBR','MSI','PLS','93');
		this.instruction_map = new Array(
			'12 or 13 numbers only', // EAN
			'11 or 12 numbers only',  // UPC
			'9 or 10 numbers only',  // ISBN
			'[0-9A-Z-. *$/+%] only',  // 39
			'ASCII code only',  // 128
			'ASCII code only',  // 128B
			'ASCII code only',  // 128C
			null, // I25
			null, // 128RAW
			null, // CBR
			'Numbers only', // MSI
			null, // PLS
			'ASCII code only' // 93
		);
		// Size node...
		this.size_node = new GFMenuNode($GFS('Set output size'));
		for(i=0;i<this.size_map.length;i++) {
			this.size_node.SetItem(i,this.size_map_text[i],size_handler);
		}
		// Code node...
		this.code_node = new GFMenuNode($GFS('Set encoding'));
		for(i=0;i<this.code_map.length;i++) {
			this.code_node.SetItem(i,this.code_map[i],code_handler);
		}
		this.menu_node = new GFMenuNode('Barcode'+$GFS('Input Method Settings'));
		this.menu_node.SetItem(0,$GFS('Set output size'),this.size_node);
		this.menu_node.SetItem(1,$GFS('Set encoding'),this.code_node);
	},
	IMName: function()
	{
		return 'Barcode';
	},
	module_name: function()
	{
		return 'BarcodeInputMethod';
	},
	GetLanguage: function()
	{
		return 'util';
	},
	GetMenuNode: function()
	{
		return this.menu_node;
	},
	UpdateUI: function()
	{
		this.ext_size.SetText(this.size_map_text[this.size_index]);
		this.ext_code.SetText(this.code_map[this.code_index]);
	},
	onActive: function()
	{
		var factory;
		var x = this;

		factory = this.ime.mainwin.GetExtensionFactory();
		this.ext_size = factory.CreateText();
		this.ext_size.SetClickHandler(function() { x.size_index = (x.size_index+1)%x.size_map.length; x.UpdateUI(); });

		this.ext_code = factory.CreateDropBox();
		this.ext_code.SetMenuNode(this.code_node);
		this.UpdateUI();
		this.ime.SetPreeditText($GFS('You can press Esc Esc 6 to configure this input method'));
	},
	onDeactive: function()
	{
		this.ext_size.Destroy();
		this.ext_code.Destroy();
		this.ext_size = null;
		this.ext_code = null;
	},
	SuggestionType: function()
	{
		return null;
	},
	isKeyValid: function(key,m)
	{
		if(m != 0 && m != Const.KEY_SHIFT) return false;

		if(32 <= key && key <= 125) return true;
		else return false;
	},
	GetHTMLPostfix: function()
	{
		return '<br />&nbsp;';
	},
	GetImageUrl: function(buf)
	{
		var size = this.size_map[this.size_index];
		var code = this.code_map[this.code_index];

		return 'http://www.ajaxime.com/~wonghang/php-barcode/barcode.php?scale='+size+'&mode=png&encoding='+code+'&code=' + encodeURI(buf);
	},
	GetTitle: function() 
	{
		return 'Barcode - ' + this.code_map[this.code_index] + ' (' + this.size_map_text[this.size_index] + ')';
	},
	GetInstruction: function()
	{
		var inst = this.instruction_map[this.code_index];
		if(inst) return $GFS('Press Enter to complete') + ' (' + inst +')';
		else return $GFS('Press Enter to complete');
	}
});
